﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace RVBike
{
    class Character
    {
        public string ImagePath { get; set; }
        public string GifPath { get; set; }
        public string Name { get; set; }
    }

    class CharacterManager : Singleton<CharacterManager>
    {
        private List<Character> _chars;

        public CharacterManager()
        {
            _chars = new List<Character>();

            if (!Directory.Exists(PathUtility.Instance().CharactersDir))
            {
                string error = String.Format("Characters folder \"{0}\" not exist", PathUtility.Instance().CharactersDir);
                Logger.Instance().Fatal(error);
                throw new System.ArgumentException(error);
            }

            ScanCharactersFolder(PathUtility.Instance().CharactersDir);
        }

        private void ScanCharactersFolder(string path)
        {
            Logger.Instance().Info("Start scanning characters folder ...");
            foreach (FileInfo fi in (new DirectoryInfo(path)).GetFiles("*.gif"))
            {
                Logger.Instance().Info(String.Format("Find character animation file : {0}", fi.Name));

                string gifPath = fi.FullName;
                string name = Path.GetFileNameWithoutExtension(fi.Name);

                // Check its corresponding image file
                string imagePath = path + name + ".jpg";
                if (!File.Exists(imagePath)) 
                {
                    Logger.Instance().Error(String.Format("Cannot find corresponding image file for animation file : {0}", fi.Name));
                }
                else
                {
                    Character aChar = new Character() 
                        {
                            Name = name,
                            ImagePath = imagePath,
                            GifPath = gifPath
                        };

                    _chars.Add(aChar);
                    Logger.Instance().Info(String.Format("Add character [{0}]", name));
                }
            }

            Logger.Instance().Info("Finish scanning characters folder ...");
        }

        public int GetCharacterCount()
        {
            return _chars.Count;
        }

        public Character GetCharByName(string name)
        {
            foreach (Character c in _chars)
            {
                if (name == c.Name)
                {
                    return c;
                }
            }
            return null;
        }

        public Character GetCharByIndex(int index)
        {
            if (index < 0 || index >= _chars.Count)
            {
                throw new System.IndexOutOfRangeException();
            }
            return _chars[index];
        }

        public int GetCharIndex(string name)
        {
            for (int i = 0 ; i < _chars.Count ; ++i)
            {
                if (name == _chars[i].Name)
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
